"""

文本情绪分析
@date 2017-1-22

"""
import traceback
import json
from QcloudApi.qcloudapi import QcloudApi

module = 'wenzhi'  # 设置需要加载的模块

action = 'TextSentiment'  # 对应接口的接口名

# 云API的公共参数

# 接口参数
config = {
    'secretId': 'AKIDkO2FKGyd4NcgZH5ETXRClJjz4ZwBc8pL',
    'secretKey': 'TlsolZGnPvNgolWUM3ax88zvhNxX2c4O'}

# 请求参数
params = {'content': '',
          'type': 2}


# 识别情绪，积极=1；消极=0
def get_emotion(content):
    params['content'] = content
    try:
        # 创建api服务对象
        service = QcloudApi(module, config)
        # 调用接口，发起请求
        result = json.loads(service.call(action, params).decode('unicode_escape'))
        print(result)
        # 请求成功
        if result['code'] == 0:
            if result['positive'] >= 0.5:
                return 1
            else:
                return 0
        else:
            return result['message']

    except Exception as e:
        print('traceback.format_exc():\n%s' % traceback.format_exc())


if __name__ == '__main__':
    print(get_emotion('你好漂亮'))
